package com.webserver.core;

import com.webserver.core.ClientHandler;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static File rootDir,staticDir;

    static {
        try {
            rootDir = new File(ClientHandler.class.getClassLoader().getResource(".").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        staticDir = new File(rootDir,"static");
    }

    public void service(HttpServletRequest request, HttpServletResponse response){
        String path = request.getUri();
        File file = new File(staticDir,path);
        System.out.println("资源是否存在："+file.exists());
        if(!file.isFile()){
            file = new File(staticDir,"root/404.html");
            response.setStatusCode(404);
            response.setStatusReason("NotFound");
        }
        response.setContentFile(file);
    }

}
